<?php

class Emms_Form_Admin_Story extends HTech_Form
{
    protected $_primary  = 'StoryId';
    
    private $_defaultClient;
    private $_defaultGroup;
    private $_defaultLocation;
    private $_defaultOfficer;
    private $_defaultReturningClient = true;
                    
    public function getValues()
    {
        $data = parent::getValues();
        unset($data['Client']);
        unset($data['Group']);
        unset($data['Location']);
        unset($data['Officer']);
        return $data;
    }
    
    private function _init()
    {
        if(!$this->_request->isPost()) {
            try {
                $storyId = (int) $this->_request->getParam('id');
                if($storyId) {
                    $stories = new Stories();
                    $story = $stories->getRecord($storyId);
                    $this->_defaultClient = $story->d->FullName;
                    $this->_defaultGroup = $story->d->GroupName;
                    $this->_defaultLocation = $story->d->Location;
                    $this->_defaultOfficer = $story->d->OfficerFullName;
                } else {
                    $clients = new Clients();
                    $client = $clients->getRecord((int) $this->_request->getParam('client'));
                    $this->_defaultClient = $client->d->FullName;
                    $this->_defaultGroup = $client->d->GroupName;
                    $this->_defaultLocation = $client->d->Location;
                    $this->_defaultOfficer = $client->d->OfficerFullName;
                }
                
            } catch (Exception $e) {
                echo $e->getMessage(); exit;
            }
        } 
    }
    
    public function __construct() 
    {
        parent::__construct();
        $this->_init();
        
        $this->setName('Story');
        
        $this->setAttrib('title', $this->translate("Story form"));
        $this->setDescription($this->translate("Edit story info or add a new one"));
        
        $storyId = new Zend_Form_Element_Hidden('StoryId');
        
        $clientId = new Zend_Form_Element_Hidden('ClientId');
        $clientId->setValue((int) $this->_request->getParam('client'));
        
        $client = new Zend_Form_Element_Text('Client');
        $client->setLabel("Client")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $group = new Zend_Form_Element_Text('Group');
        $group->setLabel("Group")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $location = new Zend_Form_Element_Text('Location');
        $location->setLabel("Location")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $officer = new Zend_Form_Element_Text('Officer');
        $officer->setLabel("Officer")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $birthPlace = new Zend_Form_Element_Text('BirthPlace');
        $birthPlace->setLabel("Birth place")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alpha', array('allowwhitespace' => true))
            ->addValidator('StringLength',false,
                array(
                    'min'      => 3,
                    'max'      => 128,
                    'messages' => $this->translate("From %min% to %max% characters only")
                )); 
        
        $growthPlace = new Zend_Form_Element_Text('GrowthPlace');
        $growthPlace->setLabel("Where were you raised")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alpha', array('allowwhitespace' => true))
            ->addValidator('StringLength',false,
                array(
                    'min'      => 3,
                    'max'      => 128,
                    'messages' => $this->translate("From %min% to %max% characters only")
                )); 
        
        $familiyInfo = new Zend_Form_Element_Textarea('FamilyInfo');
        $familiyInfo->setLabel("Family information: Children, spouse, dependents.")
            ->setRequired(true)
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $heardAboutUs = new Zend_Form_Element_Textarea('HeardAboutUs');
        $heardAboutUs->setLabel("How did you hear about us?")
            ->setRequired(true)
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $creditBeforeUs = new Zend_Form_Element_Textarea('CreditBeforeUs');
        $creditBeforeUs->setLabel("How did you get funding before?")
            ->setRequired(true)
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $businessRoutine = new Zend_Form_Element_Textarea('BusinessRoutine');
        $businessRoutine->setLabel("What is your business routine?")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        $this->_defaultReturningClient ? $businessRoutine->setRequired(true) : '';
        
        $businessProgress = new Zend_Form_Element_Textarea('BusinessProgress');
        $businessProgress->setLabel("What progress has your business seen?")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        $this->_defaultReturningClient ? $businessProgress->setRequired(true) : '';
        
        $businessChallenges = new Zend_Form_Element_Textarea('BusinessChallenges');
        $businessChallenges->setLabel("What challenges does your business have")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        $this->_defaultReturningClient ? $businessChallenges->setRequired(true) : '';
        
        $personalInterests = new Zend_Form_Element_Textarea('PersonalInterests');
        $personalInterests->setLabel("Personal interests: Sports, hobbies, little daily routine details, etc.")
            ->setRequired(true)
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $healthImpact = new Zend_Form_Element_Textarea('HealthImpact');
        $healthImpact->setLabel("Has your relationship with us impacted the family's health? Please explain.")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        $this->_defaultReturningClient ? $healthImpact->setRequired(true) : '';
        
        $housingImpact = new Zend_Form_Element_Textarea('HousingImpact');
        $housingImpact->setLabel("Has your relationship with us impacted your housing situation? Please explain.")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        $this->_defaultReturningClient ? $housingImpact->setRequired(true) : '';
        
        $familyImpact = new Zend_Form_Element_Textarea('FamilyImpact');
        $familyImpact->setLabel("How has the loan impacted your family? Please compare before and after the loan.")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        $this->_defaultReturningClient ? $familyImpact->setRequired(true) : '';
        
        $communityImpact = new Zend_Form_Element_Textarea('CommunityImpact');
        $communityImpact->setLabel("How has the loan impacted your community? Please compare before and after the loan.")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        $this->_defaultReturningClient ? $communityImpact->setRequired(true) : '';
        
        $businessImpact = new Zend_Form_Element_Textarea('BusinessImpact');
        $businessImpact->setLabel("How has the loan impacted your business? Please compare before and after the loan.")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        $this->_defaultReturningClient ? $businessImpact->setRequired(true) : '';
        
        $spiritualImpact = new Zend_Form_Element_Textarea('SpiritualImpact');
        $spiritualImpact->setLabel("How has the loan impacted your spirit? Please compare before and after the loan.")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        $this->_defaultReturningClient ? $spiritualImpact->setRequired(true) : '';
        
        $plusServices = new Zend_Form_Element_Select('PlusServices');
        $plusServices->setLabel("Have you benefit from Microfinance Plus services?")                
            ->setAttrib('size', '2') 
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addMultiOptions(array('Yes' => $this->translate("Yes"), 'No' => $this->translate("No")));   
        
        $plusServicesDetails = new Zend_Form_Element_Textarea('PlusServicesDetails');
        $plusServicesDetails->setLabel("Please explain")
            ->setRequired(true)
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $dreams = new Zend_Form_Element_Textarea('Dreams');
        $dreams->setLabel("Tell us about your dreams for the future.")
            ->setRequired(true)
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $quote = new Zend_Form_Element_Textarea('Quote');
        $quote->setLabel("Highlight a compelling quote from the client during the interview.")
            ->setRequired(true)
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $comments = new Zend_Form_Element_Textarea('Comments');
        $comments->setLabel("Interviewer general comments")
            ->setRequired(true)
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 

        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type'=>'submit')));
        
        $this->addElements(array($client,$group,$location,$officer,$birthPlace,$growthPlace,$familiyInfo,$heardAboutUs,$creditBeforeUs,
            $businessRoutine,$businessProgress,$businessChallenges,$personalInterests,$healthImpact,
            $housingImpact,$familyImpact,$communityImpact,$businessImpact,$spiritualImpact,
            $plusServices,$plusServicesDetails,$dreams,$quote,$comments,$submit,$storyId,$clientId));
        
        $this->setDefaults(array('Client' => $this->_defaultClient));
        $this->setDefaults(array('Group' => $this->_defaultGroup));
        $this->setDefaults(array('Location' => $this->_defaultLocation));
        $this->setDefaults(array('Officer' => $this->_defaultOfficer));
    }
}